package cn.dankal.flyaudio.login;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import java.util.HashMap;
import java.util.Map;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.common.sms.SmsCode;
import cn.dankal.basiclib.common.sms.SmsCodeImpl;
import cn.dankal.basiclib.pojo.PhoneOrEmailIsExistBean;
import cn.dankal.basiclib.pojo.SmSReturnBean;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.protocol.LoginProtocol;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.ReasonBean;
import cn.dankal.business.Constants;
import cn.dankal.flyaudio.R;

import static cn.dankal.basiclib.protocol.LoginProtocol.GETCODE;


@Route(path = GETCODE)
public class GetCodeActivity extends BaseActivity {

    @BindView(R.id.tv_tips)
    TextView mTvTips;
    @BindView(R.id.et_mobile)
    TextView mTvMobile;
    @BindView(R.id.et_code)
    EditText mEtCode;

    @BindView(R.id.bt_code)
    Button mBtCode;
    private String phone;
    private SmsCode smsCode;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_getcode;
    }

    @Override
    protected void initComponents() {
        type = getIntent().getStringExtra(Constants.TYPE);
        addSingleTitleBar(type);
        phone = getIntent().getStringExtra(SetLoginPasswdActivity.MOBLIE);
        mTvMobile.setText(phone);
        smsCode = new SmsCodeImpl(this);
    }

    @OnClick({R.id.bt_code, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_code:
                /**
                 * 如果当前的任务为修改密码，验证手机号码是否存在，如果存在手机号码，才可以获取验证码
                 * 如果当前任务为注册用户，验证手机号码不存在，才可以获取验证码
                 * */
                if (StringUtil.isValid(phone)) {
                    checkPhoneExits(phone, new OnCheckPhoneExistCallback() {
                        @Override
                        public void phoneIsExit() {
                            if (type.equals(Constants.TYPE_REGISTER)) {
                                ToastUtils.showShort("该手机号码已存在");
                            }
                            if (type.equals(Constants.TYPE_FORGET_PASS)) {
                                // 获取验证码
                                smsCode.getCode(phone, mBtCode, "0");
                            }
                        }

                        @Override
                        public void phoneIsNotExit() {
                            if (type.equals(Constants.TYPE_REGISTER)) {
                                // 获取验证码
                                smsCode.getCode(phone, mBtCode, "0");
                            }
                            if (type.equals(Constants.TYPE_FORGET_PASS)) {
                                ToastUtils.showShort("该手机号码不存在");
                            }
                        }
                    });
                } else {
                    ToastUtils.showShort("请填写手机号码");
                }
                break;
            case R.id.bt_next:
                String code = mEtCode.getText().toString();
                if (!StringUtil.isValid(code)) {
                    ToastUtils.showShort("请输入验证码");
                    return;
                }
//                if (type.equals(Constants.TYPE_REGISTER)) {
//                    //注册获取验证码
//                    validaCodeReigster(code);
//                } else {
//                    //忘记密码
                    validaCode(code);
//                }

                break;
            default:
        }
    }

    /**
     * 检查手机是否存在
     */
    private void checkPhoneExits(String phone, OnCheckPhoneExistCallback callback) {
        int checkType = 0;
        // 0手机 1邮箱
        UserServiceFactory.phoneOrEmailIsExist(phone, checkType)
                .subscribe(new AbstractDialogSubscriber<PhoneOrEmailIsExistBean>(this) {
                    @Override
                    public void onNext(PhoneOrEmailIsExistBean phoneOrEmailIsExistBean) {

                            int reason = Integer.valueOf(phoneOrEmailIsExistBean.getReason());
                            switch (reason) {

                                case Constants.PhoneExistStatus.isExist:
                                    callback.phoneIsExit();
                                    break;

                                case Constants.PhoneExistStatus.isNotExist:
                                    callback.phoneIsNotExit();
                                    break;
                            }

                        }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        callback.phoneIsExit();
                    }
                });
    }

    /**
     * 检查验证码
     */
    private void validaCodeReigster(String code) {

        Map<String, String> map = new HashMap(3);
        map.put(Constants.PHONE, phone);
        map.put(Constants.CODE, code);

        UserServiceFactory.registerSkip(map)
                .subscribe(new AbstractDialogSubscriber<SmSReturnBean>(this) {
                    @Override
                    public void onNext(SmSReturnBean smSReturnBean) {
                        ARouter.getInstance().build(LoginProtocol.SET_LOGIN_PASSWD)
                                .withString(SetLoginPasswdActivity.MOBLIE, phone)
                                .withString(Constants.UUID, smSReturnBean.getId())
                                .withString(Constants.CODE, code)
                                .withString(Constants.TYPE, type)
                                .navigation();

                        finish();
                    }
                });
    }


    private void validaCode(String code) {
        UserServiceFactory.validationCode(phone, "0", code)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(this) {

                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        ARouter.getInstance().build(LoginProtocol.SET_LOGIN_PASSWD)
                                .withString(SetLoginPasswdActivity.MOBLIE, phone)
                                .withString(Constants.CODE,code)
                                .withString(Constants.UUID, DKUserManager.getUserInfo().getId())
                                .withString(Constants.TYPE, type)
                                .navigation();

                        finish();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        smsCode.onDestory();
    }

    interface OnCheckPhoneExistCallback {

        void phoneIsExit();

        void phoneIsNotExit();
    }

}
